//
//  XXTGShareView.m
//  XiuXiuTuanGou
//
//  Created by 王克博 on 2018/10/18.
//  Copyright © 2018年 XiuXiuTuanGou. All rights reserved.
//

#import "XXTGShareView.h"

@interface XXTGShareView()

@property (nonatomic, strong) NSArray *icons;
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) UIButton *cancelButton;

@end

@implementation XXTGShareView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    return self;
}

- (void)createUI
{
    self.backgroundColor = [UIColor clearColor];
    
    UIView * backView = [[UIView alloc]initWithFrame:self.bounds];
    backView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    [self addSubview:backView];
    
    [self addSubview:self.shareAnimalView];
    
    _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelButton.frame = CGRectMake(10, self.shareAnimalView.bottom + 10, KScreenWidth - 20, 52);
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    _cancelButton.backgroundColor = KWhiteColor;
    [_cancelButton setTitleColor:UIColorFromRGB(0x363636) forState:UIControlStateNormal];
    _cancelButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    [self addShadowAndCircleCorner:_cancelButton.layer corner:8];
    [_cancelButton addTarget:self action:@selector(hiddenShare) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cancelButton];

    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture)];
    [backView addGestureRecognizer:tap];
}

- (void)tapGesture
{
    [self hiddenShare];
}

- (void)showShare
{
    @weakify(self);
    [UIView animateWithDuration:0.25 animations:^{
        @strongify(self);
        CGRect frame = CGRectMake(10, KScreenHeight-(256+ 75+TJ_SafeAreaBottomHeight), KScreenWidth - 20, 256);
        self.shareAnimalView.frame = frame;
        
        self.cancelButton.frame = CGRectMake(10, self.shareAnimalView.bottom + 10, KScreenWidth - 20, 52);

    }];
}


- (void)hiddenShare
{
   @weakify(self);
    [UIView animateWithDuration:0.25 animations:^{
        @strongify(self);
        CGRect frame = CGRectMake(0, KScreenHeight, KScreenWidth, 0);
        self.shareAnimalView.frame = frame;
        self.cancelButton.frame = CGRectMake(10, self.shareAnimalView.bottom + 10, KScreenWidth - 20, 0);

    } completion:^(BOOL finished) {
        @strongify(self);
        [self removeFromSuperview];
    }];
}

- (void)clickShareWeChatButton
{
    //点击分享微信好友
    if (self.clickShareWechatButtonBlock) {
        [self hiddenShare];
        self.clickShareWechatButtonBlock();
    }
}

- (void)clickShareWeChatFriendCircleButton
{
    //点击分享朋友圈
    if (self.clickShareWechatFriendCircleButtonBlock) {
        [self hiddenShare];
        self.clickShareWechatFriendCircleButtonBlock();
    }
}


//MARK: - getter
- (UIView *)shareAnimalView
{
    if (!_shareAnimalView) {
        _shareAnimalView = [[UIView alloc]initWithFrame:CGRectMake(10, KScreenHeight, KScreenWidth -20, 256)];
        _shareAnimalView.backgroundColor = [UIColor whiteColor];
        _shareAnimalView.clipsToBounds = YES;
        [self addShadowAndCircleCorner:_shareAnimalView.layer corner:8];
        
        CGFloat gap = (KScreenWidth - 67 - 50*4)/3.0;
        
        UILabel *titleLab = [UILabel makeLabelWithText:@"分享至" textColor:UIColorFromRGB(0x666666) font:mFont(14) textAliment:1];
        titleLab.frame = CGRectMake(0, 20, KScreenWidth - 20, 20);
        [_shareAnimalView addSubview:titleLab];
        
        @weakify(self);
        [self.icons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            @strongify(self);
            
            UIImageView *icon = [UIImageView new];
            icon.image = [UIImage imageNamed:obj];
            icon.size = CGSizeMake(50, 50);
            icon.mj_y = idx<4 ?60 :158;
            icon.mj_x = 20 + (50 + gap) *(idx%4);
            [self.shareAnimalView addSubview:icon];
            
            @weakify(self);
            icon.tapBlock = ^{
                @strongify(self);
                if (self.dealShareBlock) {
                    self.dealShareBlock(idx);
                }
            };
            
            UILabel *label = [UILabel makeLabelWithText:self.titles[idx] textColor:UIColorFromRGB(0x262626) font:mFont(14) textAliment:1];
            label.size = CGSizeMake(70, 20);
            label.mj_x = icon.mj_x-10;
            label.mj_y = icon.bottom + 4;
            [self.shareAnimalView addSubview:label];
            
        }];
        
    }
    return _shareAnimalView;
}

- (NSArray *)icons{
    
    if (!_icons) {
        _icons = @[@"share_wechat", @"share_wechat_circle", @"share_weibo", @"share_qq", @"share_tunji", @"share_qq_circle", @"share_copy"];
    }
    return _icons;
}

- (NSArray *)titles{
    
    if (!_titles) {
        _titles = @[@"微信", @"朋友圈", @"微博", @"QQ", @"站内豚友", @"QQ空间", @"复制链接"];
    }
    return _titles;
}


@end
